#include "frmTest_control_TPageControl.h"
using namespace frmTest_control_TPageControl;
using namespace frm;
using namespace control;

// TForm //////////////////////////////////////////////////////////////////////

//                            
TForm::TForm(TStyles &styles,frm::control::TWindow &Window) : Styles(styles),frm::control::TPanel(Window)
{
  svl::TCommand *Cmd,*Cmd3;
  MainMenu1.Items.Add(Cmd = new TCmd3);
  //MainMenu1.Items.Add(Cmd = new svl::TCommand("Menu1"));
    Cmd->Children.Add(new TCmd1);
    Cmd->Children.Add(Cmd3 = new TCmd3);
      Cmd3->Children.Add(new TCmd1);

  MainMenu1.Items.Add(Cmd = new svl::TCommand("Menu2"));
    Cmd->Children.Add(new TCmd2);
    Cmd->Children.Add(new TCmd1);
   
  MainMenu2.Items.Add(Cmd = new svl::TCommand("Page2"));
    Cmd->Children.Add(new TCmdSetFocus(*this));
    Cmd->Children.Add(new TCmd2);
    Cmd->Children.Add(new TCmd1);

  TPanel *Panel1;
  TPanelWin *PanelWin1,*PanelWin2;
  TSplitterVert *SplitterVert;

  Pages = new TPageControl(Window);
    //Pages->SetTableOfStyles(&styles.S2);
    Pages->SetRuleWidth (new geometry::TParent());
    Pages->SetRuleHeight(new geometry::TParent());
  this->AddChildLast(Pages);

    //////////////////////////
    Page1 = new TPage(Window);
      //Page1->Button->String->SetValue("Page1_qwerty");
      Page1->SetRuleWidth (new geometry::TParent());
      Page1->SetRuleHeight(new geometry::TParent());
      Page1->SetRuleDirect(new geometry::THorz());
      Page1->SetRuleWAlign(new geometry::TJustify());
      Page1->SetRuleHAlign(new geometry::TCenter());
    Pages->Client->AddChildLast(Page1);
  
        Page1->SetTableOfStyles(&Styles.S2);
        
        PanelWin1 = new TPanelWin(Window);
          //PanelWin1->SetStyle(styles.StyleW);
        Page1->AddChildLast(PanelWin1);

        PanelWin1 = new TPanelWin(Window);
          //PanelWin1->SetStyle(styles.StyleW);
          PanelWin1->SetRuleHeight(new geometry::TParent());
        Page1->AddChildLast(PanelWin1);
    
        PanelWin1 = new TPanelWin(Window);
          //PanelWin1->SetStyle(styles.StyleW);
        Page1->AddChildLast(PanelWin1);

    //////////////////////////    
    Page2 = new TPage(Window);
      //Page2->Button->String->SetValue("Page2");
      Page2->SetRuleWidth (new geometry::TParent());
      Page2->SetRuleHeight(new geometry::TParent());
      Page2->SetRuleDirect(new geometry::THorz());
      Page2->SetRuleWAlign(new geometry::TBegin());
      Page2->SetRuleHAlign(new geometry::TCenter());
    Pages->Client->AddChildBefore(Page2,Page1);

        Page2->SetTableOfStyles(&Styles.S2);

        PanelWin2 = new TPanelWin(Window);
          //PanelWin2->SetStyle(styles.StyleW);
        Page2->AddChildLast(PanelWin2);

        SplitterVert = new TSplitterVert(Window);
          SplitterVert->SetRuleHeight(new geometry::TParent());
          SplitterVert->SetCResizePrev(PanelWin2);
        Page2->AddChildLast(SplitterVert);

        Panel1 = new TPanel(Window);
          Panel1->SetWidth (200);
          Panel1->SetHeight(200);
          Panel1->SetRuleWAlign(new geometry::TCenter());
          Panel1->SetRuleHAlign(new geometry::TCenter());
        Page2->AddChildLast(Panel1);
/**********
          PanelWin2 = new TPanelWin(Window);
            PanelWin2->SetStyle(styles.StyleW);
            PanelWin2->SeRuletWidth(new geometry::TParent());
          Panel1->AddChildLast(PanelWin2);
*************/
          Edit1 = new TControlEdit(Window);
            Edit1->SetRuleHeight(new geometry::TSiblingsRem());
          Panel1->AddChildLast(Edit1);

          Edit2 = new TControlEdit(Window);
            Edit2->SetRuleHeight(new geometry::TSiblingsRem());
          Panel1->AddChildLast(Edit2);

        SplitterVert = new TSplitterVert(Window);
          //SplitterVert->SetRuleHeight(new geometry::TFixed(50));
          SplitterVert->SetCResizePrev(Panel1);
        Page2->AddChildLast(SplitterVert);
/*************************
        Panel1 = new TPanel(Window);
          Panel1->SetStyle(styles.Style1);
          Panel1->SetRuleWidth(new geometry::TSiblingsRem());
        Page2->AddChildLast(Panel1);
******************************/
        PanelWin2 = new TPanelWin(Window);
          //PanelWin2->SetStyle(styles.StyleW);
          PanelWin2->SetRuleWidth(new geometry::TSiblingsRem());
        Page2->AddChildLast(PanelWin2);

    //////////////////////////    
    Page3 = new TPage(Window);
      //Page3->Button->String->SetValue("Page3");
      Page3->SetRuleWidth (new geometry::TParent());
      Page3->SetRuleHeight(new geometry::TParent());
      Page3->SetRuleDirect(new geometry::THorz());
      Page3->SetRuleWAlign(new geometry::TBegin());
      Page3->SetRuleHAlign(new geometry::TCenter());
      Page3->SetTableOfStyles(&Styles.S3);
    Pages->Client->AddChildLast(Page3);

        TFrame *Frame = new TFrame(Window);
          Frame->SetRuleWidth (new geometry::TParent());
          Frame->SetRuleHeight(new geometry::TParent());

          Frame->Client->SetRuleWidth (new geometry::TChildrenMax());
          Frame->Client->SetRuleHeight(new geometry::TChildrenSum());
        Page3->AddChildLast(Frame);

          //TPanel *PanelF1 = new TPanel(Window);
            //PanelWin2->SetRuleWidth(new geometry::TSiblingsRem());
          //Frame->Client->AddChildLast(PanelF1);
          
          TTable *Table = new TTable(Window);
          Frame->Client->AddChildLast(Table);

            AddRow(Table->Caption,
              svl::TString() << "col1",
              svl::TString() << "col2",
              svl::TString() << "col3",
              svl::TString() << "col4"
            );

            for (int N = 0; N < 4; N++)
            {
              TRow *Row = AddRow(Table->Grid,
                svl::TString() << "col" << N << "_1",
                svl::TString() << "col" << N << "_2",
                svl::TString() << "col" << N << "_3",
                svl::TString() << "col" << N << "_4"
              );

              Row->Caption->AddChildFirst(new TRowExpand(*Row));
              for (int Nc = 0; Nc < 10; Nc++)
              {
                AddRow(Row->Children,
                  svl::TString() << "col" << N << "_x_1",
                  svl::TString() << "col" << N << "_x_2",
                  svl::TString() << "col" << N << "_x_3",
                  svl::TString() << "col" << N << "_x_4"
                );
              }
            }  
}

//
control::TRow *TForm::AddRow(control::TPanel *Parent,svl::TString C1,svl::TString C2,svl::TString C3,svl::TString C4)
{
  TWindow &Window = Parent->GetWindow();

  TRow *Row = new TRow(Window);
  Parent->AddChildLast(Row);

    TCol *Col;
    TString *String;

    Col = new TCol(Window);
      Col->SetWidth(200);
    Row->Caption->AddChildLast(Col);

      String = new TString(Window);
        String->SetValue(C1);
      Col->AddChildLast(String);

    Col = new TCol(Window);
      Col->SetWidth(200);
    Row->Caption->AddChildLast(Col);

      String = new TString(Window);
        String->SetValue(C2);
      Col->AddChildLast(String);

    Col = new TCol(Window);
      Col->SetWidth(200);
    Row->Caption->AddChildLast(Col);

      String = new TString(Window);
        String->SetValue(C3);
      Col->AddChildLast(String);

    Col = new TCol(Window);
      Col->SetWidth(200);
      //Col->SetRuleHeight(new geometry::TChildrenMax);
    Row->Caption->AddChildLast(Col);

      TParaEdit *ParaEdit = new TParaEdit(Window);
        ParaEdit->SetEnFormatting(true);
        ParaEdit->SetRuleWidth (new geometry::TParent);
        ParaEdit->SetRuleHeight(new geometry::TChildrenSum);
        ParaEdit->AddPara(C4);
      Col->AddChildLast(ParaEdit);

/**********
      String = new TString(Window);
        String->SetValue(C4);
      Col->AddChildLast(String);
**************/
      
  return Row;
}

// TPageEvents ////////////////////////////////////////////////////////////////

//
void TPageEvents::TEvents_BeforeChangeVisible(TChangeVisible &Param) 
{ 
  TPage *Page = dynamic_cast<TPage*>(&Param.Sender); if (Page) {
/************
    svl::TMessage::AddLine(
      "----------------------------------"
    );
    svl::TMessage::AddLine(
      Page->Button->String->GetValue() 
      << "(Visible=" << Param.Visible 
      << " VisiblePage=" << Form.Pages->GetVisiblePage() 
      << ")"
    );
************/    
  }
  TEvents::TEvents_BeforeChangeVisible(Param);
}

//
void TPageEvents::TEvents_AfterChangeVisible(TChangeVisible &Param) 
{ 
  TEvents::TEvents_AfterChangeVisible(Param);
}

// TPageControlEvents /////////////////////////////////////////////////////////

//
void TPageControlEvents::TEvents_BeforeShowPage(TShowPage &Param)
{
  if (Form.Page1 == Param.Page) {
    Form.MainMenu1.SetToWindow(Param.Page->GetWindow());
  }
  else if (Form.Page2 == Param.Page) {
    //Param.EnShow = false;
    Form.MainMenu2.SetToWindow(Param.Page->GetWindow());
  }
/**************
  svl::TMessage::AddLine(
    "--TPageControlEvents::TEvents_BeforeShowPage"
  );
  svl::TMessage::AddLine(svl::TString() 
    << "  " << Param.Page->Button->String->GetValue()
  );
*****************/
  TEvents::TEvents_BeforeShowPage(Param);
}

//
void TPageControlEvents::TEvents_AfterShowPage(TShowPage &Param)
{
  TEvents::TEvents_AfterShowPage(Param);
}

// TWindowEvents //////////////////////////////////////////////////////////////

//
void TWindowEvents::TEvents_BeforeClose(TClose &Param)
{
  //Param.EnClose = false;
  TEvents::TEvents_BeforeClose(Param);
}

///////////////////////////////////////////////////////////////////////////////

void frmTest_control_TPageControl::Test()
{
  frm::control::TWindow Win;

  TStyles Styles;
  TForm Form(Styles,Win);
  TPageEvents Ev1(Form);
  TPageControlEvents Ev2(Form);
  TWindowEvents Ev3(Form);
  
  Form.SetRuleWidth (new geometry::TParent());
  Form.SetRuleHeight(new geometry::TParent());
  Win.Client.SetTableOfStyles(&Styles.S1);

  Win.Client.AddChildLast(&Form,false);

  Win.Client.InheritStyle();
  Form.Page1->UpdateVisible(true);

  Win.SetWindowRect(grf::TRect(100,100,600,600),false);
  Win.Show();
  Win.MessagesLoop();
  
  Win.Client.DeleteChildren();
}
